package com.securitydome09.services;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.log.LogMessage;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.rememberme.*;

public class MyRememberMeServices extends PersistentTokenBasedRememberMeServices {


    private PersistentTokenRepository tokenRepository = new InMemoryTokenRepositoryImpl();


    public MyRememberMeServices(String key, UserDetailsService userDetailsService, PersistentTokenRepository tokenRepository) {
        super(key, userDetailsService, tokenRepository);
    }


    /**
     * 自定义前后端分离获取 remember-me的方式
     * @param request
     * @param parameter
     * @return
     */
    @Override
    protected boolean rememberMeRequested(HttpServletRequest request, String parameter) {

        String paramValue = (String) request.getAttribute(parameter);
        System.out.println(parameter+":"+paramValue);
        if (paramValue == null ||
                !paramValue.equalsIgnoreCase("true") &&
                        !paramValue.equalsIgnoreCase("on") &&
                        !paramValue.equalsIgnoreCase("yes") &&
                        !paramValue.equals("1")) {
            this.logger.debug(LogMessage.format("Did not send remember-me cookie (principal did not set parameter '%s')", parameter));
            System.out.println("false");
            return false;
        } else {
            return true;
        }
    }








}
